package com.panda.pandawash.fragment;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.panda.pandawash.util.ProductsListUtils;
import com.panda.pandawash.R;
import com.panda.pandawash.activity.MotorMonitorActivity;
import com.panda.pandawash.model.MotorDeviceInfoMessage;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by gaowf on 2017/2/25.
 */

public class MonitorInfoFragment extends Fragment {

    private String TAG = "MonitorUserFragment";

    public final static int CONNECTED_FLAG = 0x10;
    public final static int UPDATE_DEV_INFO = 0x21;
    public final static String MOTOR_RUNING = "runing";
    public final static String MOTOR_DEV_INFO = "devinfo";
    public final static String MOTOR_PRODUCTOR = "productor";
    public final static String MOTOR_PRODUCT_DATE = "product_date";
    public final static String MOTOR_PRODUCT_VERSION = "product_version";
    public final static String MOTOR_STATUS_INFO = "devstatus";
    private int list_pos = 0;

    private TextView mMotorProductTextView;
    private TextView mMotorDateTextView;
    private TextView mMotorVersonTextView;
    private TextView mMotorproductrealTextView;
    private TextView mMotorSerialIdTextView;


    private TextView mMotorTypeTextView;
    private TextView mMotorKwTextView;
    private TextView mMotorPressureTextView;
    private TextView mMotorLminTextView;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.monitor_user_layout);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View fragmentView = inflater.inflate(R.layout.monitor_user_layout,
                container, false);
        Log.d(TAG, "MonitorUserFragment");
        mMotorDateTextView = (TextView) fragmentView
                .findViewById(R.id.motordate);
        mMotorProductTextView = (TextView) fragmentView
                .findViewById(R.id.motorproduct);
        mMotorVersonTextView = (TextView) fragmentView
                .findViewById(R.id.motorversion);

        mMotorproductrealTextView = (TextView)fragmentView.findViewById(R.id.motor_product_real);

        mMotorSerialIdTextView = (TextView)fragmentView.findViewById(R.id.motor_serial_id);

        mMotorTypeTextView = (TextView)fragmentView.findViewById(R.id.device_type);
        mMotorKwTextView = (TextView)fragmentView.findViewById(R.id.device_kw);
        mMotorPressureTextView = (TextView)fragmentView.findViewById(R.id.device_presseure);
        mMotorLminTextView = (TextView)fragmentView.findViewById(R.id.device_lmin);


        return fragmentView;
    }

    Handler mHandler = new Handler() {

        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case CONNECTED_FLAG:

                    break;
                case UPDATE_DEV_INFO: {

                    String statusinfo = msg.getData().getString(MOTOR_STATUS_INFO);

                    Log.i(TAG, statusinfo);

                    try {
                        JSONObject object = new JSONObject(statusinfo);

                        String washer_productorString  = object.getString(MotorDeviceInfoMessage.MOTOR_PRODUCTOR_NAME);
                        String washer_product_dateString = object.getString(MotorDeviceInfoMessage.MOTOR_PRODUCT_DATE);
                        String washer_product_real = object.getString(MotorDeviceInfoMessage.MOTOR_CHECK_REAL);
                        String washer_product_version = object.getString(MotorDeviceInfoMessage.MOTOR_VERSION);

                        String washer_Serial_id = object.getString(MotorDeviceInfoMessage.MOTOR_SERIAL_ID);

                        mMotorProductTextView.setText(washer_productorString);
                        mMotorDateTextView.setText(washer_product_dateString);
                        mMotorVersonTextView.setText(washer_product_version);
                        mMotorproductrealTextView.setText(washer_product_real);
                        mMotorSerialIdTextView.setText(washer_Serial_id);

                        List<Map<String, Object>> infoMaps = ProductsListUtils.getProductsList();

                        HashMap<String, Object> hashMap = (HashMap<String, Object>) infoMaps.get(list_pos);


                        mMotorTypeTextView.setText(hashMap.get("device_name").toString());
                        mMotorKwTextView.setText(hashMap.get("device_power").toString() + " KW");
                        mMotorLminTextView.setText(hashMap.get("device_flow").toString() + "L/min");


                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }
                break;
                default:
                    break;
            }
        };
    };

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub

        switch (resultCode) {
            case CONNECTED_FLAG:
                break;
            case UPDATE_DEV_INFO:
                updateInfo(data);
                break;
            default:
                break;
        }

    }

    private void updateInfo(Intent data) {

        String productorString = data.getStringExtra(MOTOR_STATUS_INFO);
        list_pos = data.getIntExtra(MotorMonitorActivity.EXTRAS_DEVICE_MODEL, 0);
        Message message = new Message();
        message.what = UPDATE_DEV_INFO;
        byte[] msg = data.getByteArrayExtra(MOTOR_STATUS_INFO);
        Bundle bundle = new Bundle();

        bundle.putString(MOTOR_STATUS_INFO, productorString);
        message.setData(bundle);
        mHandler.sendMessage(message);

    }
}

